<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    append-to-body
    :title="title"
  >
    <el-form :model="form" ref="formRef" label-width="110px">
      <el-form-item
        label="优惠券"
        prop="coupon_id"
        :rules="[
          { required: true, message: '请选择优惠券', trigger: 'change' },
        ]"
      >
        <el-select
          v-model="form.coupon_id"
          clearable
          filterable
          placeholder="请选择优惠券"
        >
          <el-option
            v-for="item in couponData"
            :key="item.id"
            :label="item.coupon_name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="领取截止时间"
        prop="receive_end_time"
        :rules="[
          { required: true, message: '请输入领取截止时间', trigger: 'blur' },
        ]"
      >
        <el-date-picker
          v-model="form.receive_end_time"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请输入领取截止时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="使用截止时间"
        prop="use_end_time"
        :rules="[
          { required: true, message: '请输入使用截止时间', trigger: 'blur' },
        ]"
      >
        <el-date-picker
          v-model="form.use_end_time"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请输入使用截止时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="发放数量"
        prop="count"
        :rules="[
          { required: true, message: '请输入发放数量', trigger: 'blur' },
        ]"
      >
        <el-input
          type="number"
          placeholder="请输入发放数量"
          v-model="form.count"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="每天定时发放"
        prop="daily"
      >
        <el-checkbox v-model="form.daily"></el-checkbox>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { debounce } from "@/util";
import { ApiGetCouponSend } from "@/api/index.js";
export default {
  components: {},
  props: ["couponData"],
  data() {
    return {
      form: {
        coupon_id: "",
        receive_end_time: "",
        use_end_time: "",
        count: "",
        daily:false
      },
      btnLoading: false,
      dialogVisible: false,
      title: "发放优惠券",
    };
  },

  methods: {
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;
            try {
              await ApiGetCouponSend(this.form);
              this.$message.success("发放成功");
              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show() {
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = {
        coupon_id: "",
        receive_end_time: "",
        use_end_time: "",
        count: "",
        daily:false
      };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
      });
    },
  },
};
</script>

<style></style>
